安裝Flask
pip install flask
建構資料夾
static/images
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PTT 文章分析結果</title>
</head>
<body>
<h1>PTT 文章數據分析結果</h1>
<p>文章總數: {{ total_articles }}</p>
<p>平均每篇文章的圖片數量: {{ avg_image_count }}</p>
<p>平均每篇文章的表格數量: {{ avg_table_count }}</p>
<h2>圖片數量分佈圖</h2>
<img src="/static/images/image_distribution.png" alt="圖片數量分佈圖">
<h2>表格數量分佈圖</h2>
<img src="/static/images/table_distribution.png" alt="表格數量分佈圖">
<h2>圖片數量與表格數量的關係圖</h2>
<img src="/static/images/image_table_relationship.png" alt="圖片數量與表格數量的關係圖">
</body>
</html>
import pandas as pd
import matplotlib.pyplot as plt
from flask import Flask, render_template
app = Flask(__name__)
# 從包含 image_count 和 table_count 列的CSV文件讀取數據
df = pd.read_csv('ptt_articles_with_counts.csv')
# 計算統計數據
total_articles = len(df)
avg_image_count = df['image_count'].mean()
avg_table_count = df['table_count'].mean()
# 生成圖表並保存到靜態文件夾
plt.figure(figsize=(10, 6))
plt.hist(df['image_count'], bins=range(0, df['image_count'].max() + 2), color='skyblue', edgecolor='black')
plt.title('Distribution of Image Counts in Articles')
plt.xlabel('Number of Images')
plt.ylabel('Number of Articles')
plt.grid(True)
plt.savefig('static/images/image_distribution.png')
plt.figure(figsize=(10, 6))
plt.hist(df['table_count'], bins=range(0, df['table_count'].max() + 2), color='lightgreen', edgecolor='black')
plt.title('Distribution of Table Counts in Articles')
plt.xlabel('Number of Tables')
plt.ylabel('Number of Articles')
plt.grid(True)
plt.savefig('static/images/table_distribution.png')
plt.figure(figsize=(10, 6))
plt.scatter(df['image_count'], df['table_count'], color='coral')
plt.title('Relationship between Image Counts and Table Counts')
plt.xlabel('Number of Images')
plt.ylabel('Number of Tables')
plt.grid(True)
plt.savefig('static/images/image_table_relationship.png')
@app.route('/')
def index():
return render_template('index.html',
total_articles=total_articles,
avg_image_count=avg_image_count,
avg_table_count=avg_table_count)
if __name__ == '__main__':
app.run(debug=True)